#!/usr/bin/env python
# encoding: utf-8
"""
Shovelhead
File: Gear.py
Author: Myriam Boucher

Manages the shifting of the motorcycle.
"""

from pyo import *
import time

class Gear:
    def __init__(self, speed, objEngine):
   
        self.motor = objEngine
        self.mix = self.motor.mix
        self.speed = speed
        self.compteur = 0  # to count the gear changes.
        self.expSpeed = None
        
    def startingOut(self):
        # the engine starts.
        self.motor.changeSpeed(0)
        self.speed = 0
        self.motor.outEngine()
        self.expSpeed = Expseg([(0,.01),(1,0.35),(1.5,2.1), (2.5,.7), (2.7,1.2), (2.9,.5),(3,1.55),(3.1,.6)],
            loop=False, exp=.5, inverse=False, initToFirstVal=False, mul=1, add=0).play()
        self.motor.changeSpeed(self.expSpeed)  # shift applied to the motor.
        #self.expSpeed.graph(xlen=None, yrange=None, title=None, wxnoserver=False)  #- to see the graph shift
        self.setSpeed(1)
        
        
    def shift(self):
        # manages gear changes.
        if self.speed == 1: 
            self.expSpeed = Expseg([(0,.4), (.7,1.2), (2.8,2), (3.5,.9)], loop=False, exp=.5, inverse=False,
                initToFirstVal=False, mul=1, add=0).play()
            self.motor.changeSpeed(self.expSpeed)
            self.compteur = 0
            
        elif self.speed == 2:
            self.expSpeed = Expseg([(0,.3), (1, 1.45), (1.2,1.20),(5,2.3), (5.2,1.5)], loop=False, exp=.5, inverse=False,
                initToFirstVal=False, mul=1, add=0).play()
            self.motor.changeSpeed(self.expSpeed)
            
        elif self.speed == 3:
            self.expSpeed = Expseg([(0,.35), (1,1.55), (1.25,1.30), (5.8,2.35),(6,1.6)], loop=False, exp=.5, inverse=False,
                initToFirstVal=False, mul=1, add=0).play()
            self.motor.changeSpeed(self.expSpeed)
            
        elif self.speed == 4:
            self.compteur = 1
            self.expSpeed = Expseg([(0,.35), (1.2, 1.95), (1.3, 1.7), (6.8,2.4),(7,1.95)], loop=False, exp=.5, inverse=False,
                initToFirstVal=False, mul=1, add=0).play()
            self.motor.changeSpeed(self.expSpeed)
            
        #self.expSpeed.graph(xlen=None, yrange=None, title=None, wxnoserver=False)# - to see the graph shift
        
        if self.compteur==0:
            self.speed += 1  
        else:
            self.speed -= 1
    
    def stopExpSpeed(self):
        # stop the Expseg object.
        if self.expSpeed != None:
            self.expSpeed.stop()  
        
    def setSpeed(self, x):
        # set the speed of the engine, [1,4].
        self.speed = x